package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.CategoryTypeController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.CategoryTypeManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.CategoryTypeJson;
import com.hanoi.wiki.model.json.ResponseJsonObject;

@Controller
@RequestMapping("/ctp")
public class CategoryTypeControllerImpl extends BaseControllerImpl implements CategoryTypeController{

	@Autowired
	private CategoryTypeManager typeManager;
	
	public @ResponseBody ResponseJsonObject saveCategoryType(@RequestBody CategoryTypeJson categoryType) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		int id = 0;
		try {
			id = typeManager.save(categoryType);
		} catch (FunctionalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		categoryType.setId(id);
		objects.add(categoryType);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeCategoryType(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		CategoryTypeJson json = null;
		
		try {
			json = typeManager.get(id);
			typeManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateCategoryType(@RequestBody CategoryTypeJson categoryType) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			typeManager.update(categoryType);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(categoryType);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getCategoryType(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		CategoryTypeJson json = null;
		try {
			json = typeManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getCategoryTypeList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(typeManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

}
